package com.media.publish;

import android.view.View;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.SeekBar;
import android.widget.TextView;
import android.widget.VideoView;

import com.baselibrary.activity.BaseActivity;
import com.baselibrary.utils.ToastUtils;
import com.media.AlbumManageUtils;
import com.media.R;
import com.media.databinding.ActivityPreViewVideoBinding;

public class PreViewVideoActivity extends BaseActivity {

    private ActivityPreViewVideoBinding mBinding;
    private int mSlidePro;
    private Thread mVideoThread;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_pre_view_video;
    }

    @Override
    protected void init() {
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        mBinding = getDataBinding();
        initData();
        initListener();
    }


    private void initData() {
        String mVideoPath = getIntent().getExtras().getString("preVideoPath");
        mBinding.vvPreVideo.setVideoPath(mVideoPath);//设置视频文件
        mBinding.vvPreVideo.start();
        mBinding.ivPreVideo.setSelected(true);
        mBinding.ivPreVideo.setVisibility(View.GONE);
        setVideoListener(mBinding.vvPreVideo, mBinding.ivPreVideo, mBinding.tvPreCurrenttime, mBinding.tvPreTotaltime, mBinding.sbPre);
    }

    private void initListener() {
        mBinding.ivPreBack.setOnClickListener(view -> finish());
        mBinding.vvPreVideo.setOnClickListener(v -> {
            if (mBinding.vvPreVideo.isPlaying()) {
                mBinding.vvPreVideo.pause();
                mBinding.ivPreVideo.setSelected(false);
                mBinding.ivPreVideo.setVisibility(View.VISIBLE);
            } else {
//                mBinding.ivPreCover.setVisibility(View.GONE);
//                mBinding.vvPreVideo.setVisibility(View.VISIBLE);
                mBinding.vvPreVideo.start();
                mBinding.ivPreVideo.setSelected(true);
                mBinding.ivPreVideo.setVisibility(View.GONE);
            }
        });
    }

    private void setVideoListener(final VideoView mVvPreviewVideo, final ImageView mIvVideoControl,final TextView mTvCurrentSpeed, final TextView mTvTotalSpeed, final SeekBar mSbSpeed) {
        mVvPreviewVideo.setOnCompletionListener(mp -> {
            mp.start();
            mIvVideoControl.setSelected(true);
        });
        mVvPreviewVideo.setOnPreparedListener(mediaPlayer -> {
            int duration = mVvPreviewVideo.getDuration();
            mTvTotalSpeed.setText(AlbumManageUtils.stringForTime(duration));
            mSbSpeed.setMax(duration);
            mediaPlayer.setOnSeekCompleteListener(mediaPlayer1 -> {
                mIvVideoControl.setSelected(true);
            });
        });
        mVvPreviewVideo.setOnErrorListener((mp, what, extra) -> {
            mIvVideoControl.setSelected(false);
            ToastUtils.showShort(getApplicationContext(), "播放出错");
            //异常回调
            return true;//如果方法处理了错误，则为true；否则为false。返回false或根本没有OnErrorListener，将导致调用OnCompletionListener。
        });
        mSbSpeed.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean b) {
                mSlidePro = progress;
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {
                //按住进度条停止播放
                mVvPreviewVideo.pause();
            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {
                //手指抬起获取当前SeekBar进度
                mVvPreviewVideo.seekTo(mSlidePro);
                mVvPreviewVideo.start();
            }
        });
        mVideoThread = new Thread() {
            @Override
            public void run() {
                try {
                    while (true) {
                        if (mVvPreviewVideo.isPlaying()) {
                            // 如果正在播放，没500.毫秒更新一次进度条
                            mTvCurrentSpeed.post(() -> {
                                int current = mVvPreviewVideo.getCurrentPosition();
                                mTvCurrentSpeed.setText(AlbumManageUtils.stringForTime(current));
                                mSbSpeed.setProgress(current);
                            });
                        }
                        sleep(500);
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        };
        mVideoThread.start();

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
            mBinding.vvPreVideo.pause();
            mBinding.vvPreVideo.stopPlayback();
        if (mVideoThread != null && mVideoThread.isAlive()) {
            mVideoThread.interrupt();
        }
    }
}
